﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
using ThanNhanTao.Properties;
using ThanNhanTao.CommonControl;
using ThanNhanTao.Server;
using ThanNhanTao.Server.Enum;
using DevExpress.Xpf.Grid;


namespace ThanNhanTao
{
    /// <summary>
    /// Interaction logic for frmCauHinhHanBHYT.xaml
    /// </summary>
    public partial class frmThongBaoHanBHYT : DXWindow
    {
        public List<BenhNhan> listBNDangDieuTri = new List<BenhNhan>();
        DateTime ngayHetHan;
        MainWindow mainWindow;
        public frmThongBaoHanBHYT(MainWindow mainWindow)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
            try
            {
                int soNgayHetHan = Settings.Default.SoNgayHetHan;
                ngayHetHan = Convert.ToDateTime(DateTime.UtcNow.AddDays(soNgayHetHan).ToShortDateString() + " 00:00:00");
                lblSoNgayHetHan.Content = String.Format("Thông báo trước {0} ngày so với thời gian hết hạn BHYT {1}"
                                                                , soNgayHetHan, ngayHetHan.ToString("dd/MM/yyyy"));
                BindDanhSachBNHetHanBHYTT();
            }
            catch (Exception)
            {
                DXMessageBox.Show("Kiểm tra không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public void BindDanhSachBNHetHanBHYTT()
        {
            listBNDangDieuTri = ThanNhanTaoGlobal.DanhSachBenhNhan.Where(u => u.TrangThaiDieuTri == EnumTrangThaiBenhNhan.DangDieuTriString).ToList();
            listBNDangDieuTri = (from a in listBNDangDieuTri
                                 where a.SoBHYT.Length > 0
                                       && (a.DenNgay != null ? a.DenNgay.Value <= ngayHetHan : false)
                                 select a).ToList();
            gridBenhNhan.ItemsSource = listBNDangDieuTri;
        }

        private void btnDong_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void DXWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Escape)
                this.Close();
        }

        private void TableView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (e.RightButton == MouseButtonState.Pressed) return;
            if (gridBenhNhan.View.GetRowHandleByMouseEventArgs(e) != GridControl.InvalidRowHandle
                && gridBenhNhan.View.GetRowHandleByMouseEventArgs(e) != GridControl.AutoFilterRowHandle
                && gridBenhNhan.View.GetRowHandleByMouseEventArgs(e) != GridControl.NewItemRowHandle)
            {
                BenhNhan benhNhan = (BenhNhan)gridBenhNhan.GetFocusedRow();
                frmPhieuBenhNhan frmUp = new frmPhieuBenhNhan(mainWindow, benhNhan);
                frmUp.ShowDialog();
                if (frmUp.daLuuThanhCong)
                    BindDanhSachBNHetHanBHYTT();
            }
        }
    }
}
